package com.kobe.project.utils;

import com.kobe.project.domain.MailBean;
import lombok.RequiredArgsConstructor;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MailUtil {
    /**
     * 邮件发送者
     */
    @Value("${spring.mail.username}")
    private String MAIL_SENDER;

    /**
     * 注入QQ发送邮件的bean
     */
    private final JavaMailSender javaMailSender;

    public void sendMail(String mail,String content) {
        MailBean mailBean = new MailBean();
        //接收者
        mailBean.setRecipient(mail);
        //标题
        mailBean.setSubject("牢大工程消息");
        //内容主体
        mailBean.setContent(content);
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送者
            mailMessage.setFrom(MAIL_SENDER);
            //接收者
            mailMessage.setTo(mailBean.getRecipient());
            //邮件标题
            mailMessage.setSubject(mailBean.getSubject());
            //邮件内容
            mailMessage.setText(mailBean.getContent());
            //发送邮箱
            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

